<?php 
    session_start(); 
    include("../include/util.inc.php");
    /****************************
    *Function name: updateQuz
    *Description: Overwrite the corresponding record 
                in the database question table with the 
                new information according to the question id.
    *Parameters: questionId, questionCategory, questionLevel, 
                questionContext, questionAnswer, questionProperty
    *Return: message
    *******************************/
    function updateQuz($id, $category, $level, $quz, $ans, $property){
        $pdo = getPDO();
        $category = $pdo->quote($category);
        $quz = $pdo->quote($quz);
        $ans = $pdo->quote($ans);
        if($property == 1)$property = $_SESSION['uid'];
        $sql = "UPDATE question SET category=$category,level=$level,question=$quz,answer=$ans,property=$property WHERE id=$id";
        $result = $pdo->query($sql);
        $message = "null";
        if($result->rowCount()>=1) $message = "success";
        else $message = "error";
        return $message;
    }
    /** 
    *Description: Receive the information of questuin 
    *submitted by js and call the function to update it.
    */
    try{
        $id = $_POST['id'];
        $category = $_POST['category'];
        $level = $_POST['level'];
        $quz = $_POST['question'];
        $ans = $_POST['answer'];
        $property = $_POST['property'];
        echo updateQuz($id, $category, $level, $quz, $ans, $property);
    }catch (\PDOException $e) {
        throw new \PDOException($e->getMessage(), (int) $e->getCode());
    }
?>